package com.lzb.watermarkcamera.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.watermarkcamera.R;
import com.lzb.watermarkcamera.adapter.ColorAdapter;
import com.lzb.watermarkcamera.basic.dialog.BaseSmartDialog;
import com.lzb.watermarkcamera.bean.ColorBean;
import com.lzb.watermarkcamera.bean.MeetingSummaryBean;
import com.lzb.watermarkcamera.databinding.DialogEditTemplate3Binding;
import com.lzb.watermarkcamera.utils.AppSpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class EditTemplate3Dialog extends BaseSmartDialog<DialogEditTemplate3Binding> implements View.OnClickListener, OnItemClickListener {

    private MeetingSummaryBean meetingSummaryBean;
    private ColorAdapter colorAdapter;
    private List<ColorBean> listColor;
    private int colorPos;
    private SelTemplateDateDialog selTemplateDateDialog;


    public EditTemplate3Dialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_edit_template3;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
        mDataBinding.tvDate.setOnClickListener(this);
        mDataBinding.ivShowSubtitle.setOnClickListener(this);
        mDataBinding.ivShowDate.setOnClickListener(this);
        mDataBinding.ivShowHost.setOnClickListener(this);
        mDataBinding.ivShowRecorder.setOnClickListener(this);
        mDataBinding.ivShowParticipant.setOnClickListener(this);
        mDataBinding.ivShowLocation.setOnClickListener(this);
        mDataBinding.ivShowTheme.setOnClickListener(this);

        mDataBinding.rvColorList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        colorAdapter = new ColorAdapter();
        mDataBinding.rvColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);

        meetingSummaryBean = AppSpUtils.getMeetingSummary();
        mDataBinding.etTitle.setText(meetingSummaryBean.getTitle());
        mDataBinding.etSubtitle.setText(meetingSummaryBean.getSubtitle());
        mDataBinding.etHost.setText(meetingSummaryBean.getReporter());
        mDataBinding.etRecorder.setText(meetingSummaryBean.getRecorder());
        mDataBinding.etParticipant.setText(meetingSummaryBean.getParticipants());
        mDataBinding.etLocation.setText(meetingSummaryBean.getLocation());
        mDataBinding.etTopic.setText(meetingSummaryBean.getTopic());

        if (TextUtils.isEmpty(meetingSummaryBean.getDate())) {
            mDataBinding.tvDate.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
        } else {
            mDataBinding.tvDate.setText(meetingSummaryBean.getDate());
        }

        // 初始化显示/隐藏状态图标
        mDataBinding.ivShowSubtitle.setImageResource(meetingSummaryBean.isShowReporter() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowDate.setImageResource(meetingSummaryBean.isShowDate() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowHost.setImageResource(meetingSummaryBean.isShowReporter() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowRecorder.setImageResource(meetingSummaryBean.isShowRecorder() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowParticipant.setImageResource(meetingSummaryBean.isShowParticipants() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowLocation.setImageResource(meetingSummaryBean.isShowLocation() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowTheme.setImageResource(meetingSummaryBean.isShowTopic() ? R.drawable.akai : R.drawable.aguan);

        listColor = new ArrayList<>();
        listColor.add(new ColorBean("#000000", false));
        listColor.add(new ColorBean("#D84545", false));
        listColor.add(new ColorBean("#FFA600", false));
        listColor.add(new ColorBean("#FAFD00", false));
        listColor.add(new ColorBean("#13FD00", false));
        listColor.add(new ColorBean("#00F5FF", false));
        listColor.add(new ColorBean("#4B6DFF", false));
        listColor.add(new ColorBean("#FF29FD", false));

        for (int i = 0; i < listColor.size(); i++) {
            ColorBean colorBean = listColor.get(i);
            if (colorBean.getColor().equals(meetingSummaryBean.getThemeColor())) {
                colorPos = i;
                colorBean.setSelected(true);
                break;
            }
        }
        colorAdapter.setList(listColor);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        } else if (id == R.id.tvDate) {
            showSelDateDialog();
        } else if (id == R.id.ivShowSubtitle) {
            meetingSummaryBean.setShowReporter(!meetingSummaryBean.isShowReporter());
            mDataBinding.ivShowSubtitle.setImageResource(meetingSummaryBean.isShowReporter() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowDate) {
            meetingSummaryBean.setShowDate(!meetingSummaryBean.isShowDate());
            mDataBinding.ivShowDate.setImageResource(meetingSummaryBean.isShowDate() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowHost) {
            meetingSummaryBean.setShowReporter(!meetingSummaryBean.isShowReporter());
            mDataBinding.ivShowHost.setImageResource(meetingSummaryBean.isShowReporter() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowRecorder) {
            meetingSummaryBean.setShowRecorder(!meetingSummaryBean.isShowRecorder());
            mDataBinding.ivShowRecorder.setImageResource(meetingSummaryBean.isShowRecorder() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowParticipant) {
            meetingSummaryBean.setShowParticipants(!meetingSummaryBean.isShowParticipants());
            mDataBinding.ivShowParticipant.setImageResource(meetingSummaryBean.isShowParticipants() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowLocation) {
            meetingSummaryBean.setShowLocation(!meetingSummaryBean.isShowLocation());
            mDataBinding.ivShowLocation.setImageResource(meetingSummaryBean.isShowLocation() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowTheme) {
            meetingSummaryBean.setShowTopic(!meetingSummaryBean.isShowTopic());
            mDataBinding.ivShowTheme.setImageResource(meetingSummaryBean.isShowTopic() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.tvConfirm) {
            saveData();
            dismiss();
            if (listener != null) {
                listener.clickConfirm();
            }

        }
    }

    private void saveData() {
        String title = mDataBinding.etTitle.getText().toString();
        String subtitle = mDataBinding.etSubtitle.getText().toString();
        String date = mDataBinding.tvDate.getText().toString();
        String reporter = mDataBinding.etHost.getText().toString();
        String recorder = mDataBinding.etRecorder.getText().toString();
        String participants = mDataBinding.etParticipant.getText().toString();
        String location = mDataBinding.etLocation.getText().toString();
        String topic = mDataBinding.etTopic.getText().toString();

        if (TextUtils.isEmpty(title)) {
            title = "";
        }
        if (TextUtils.isEmpty(subtitle)) {
            subtitle = "";
        }
        if (TextUtils.isEmpty(reporter)) {
            reporter = "";
        }
        if (TextUtils.isEmpty(recorder)) {
            recorder = "";
        }
        if (TextUtils.isEmpty(participants)) {
            participants = "";
        }
        if (TextUtils.isEmpty(location)) {
            location = "";
        }
        if (TextUtils.isEmpty(topic)) {
            topic = "";
        }

        meetingSummaryBean.setTitle(title);
        meetingSummaryBean.setSubtitle(subtitle);
        meetingSummaryBean.setDate(date);
        meetingSummaryBean.setReporter(reporter);
        meetingSummaryBean.setRecorder(recorder);
        meetingSummaryBean.setParticipants(participants);
        meetingSummaryBean.setLocation(location);
        meetingSummaryBean.setTopic(topic);

        AppSpUtils.saveMeetingSummary(meetingSummaryBean);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        listColor.get(colorPos).setSelected(false);
        colorPos = position;
        listColor.get(colorPos).setSelected(true);
        colorAdapter.notifyItemChanged(position);
        meetingSummaryBean.setThemeColor(listColor.get(position).getColor());
    }

    private void showSelDateDialog() {
        if (selTemplateDateDialog == null) {
            selTemplateDateDialog = new SelTemplateDateDialog(getContext());
            selTemplateDateDialog.setDate(mDataBinding.tvDate.getText().toString());
        }
        selTemplateDateDialog.setListener(new SelTemplateDateDialog.Ilistener() {
            @Override
            public void clickConfirm(long date) {
                mDataBinding.tvDate.setText(TimeUtils.millis2String(date, new SimpleDateFormat("yyyy-MM-dd")));
            }
        });
        selTemplateDateDialog.show();
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm();
    }
}